package com.wx.controller;


import com.wx.dto.BookPageQueryDTO;
import com.wx.dto.BookTypeDTO;
import com.wx.entity.Book;
import com.wx.entity.BookType;
import com.wx.result.PageResult;
import com.wx.result.Result;
import com.wx.service.BookService;
import com.wx.vo.BookTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/book")
@Slf4j
/* 书籍信息控制类 */
public class BookController {
    @Autowired
    private BookService bookService;

    /*图书分页查询*/
    @PostMapping("/list")
    private Result<PageResult> getbooklist(@RequestBody BookPageQueryDTO bookPageQueryDTO){
        log.info("查询图书信息{}",bookPageQueryDTO);
        PageResult pageResult = bookService.queryBook(bookPageQueryDTO);
        return Result.success(pageResult);

    }

    @GetMapping("/add")
    private Result addbook(Book book){
        log.info("添加图书"+book);
        bookService.addbook(book);
        return Result.success();
    }

    @PutMapping("/addtype")
    private Result addtype(@RequestBody BookTypeDTO bookTypeDTO){
        log.info("增加图书分类"+ bookTypeDTO);
        bookService.addtype(bookTypeDTO);
        return Result.success();
    }

    @GetMapping("/typelist")
    private Result getType(BookTypeDTO bookTypeDTO){
        log.info("图书类型查询{}",bookTypeDTO);
        List<BookTypeVO> booktypeVO = bookService.getType(bookTypeDTO);
        return Result.success(booktypeVO);
    }

    @GetMapping("/gettype")
    private Result getTypeByid(Integer id){
        log.info("根据id查询分类"+id);
        BookType type = bookService.getTypeByid(id);
        return Result.success(type);
    }

    @PutMapping("/updatetype")
    private Result updatetype(@RequestBody BookTypeDTO bookTypeDTO){
        log.info("修改分类信息：" + bookTypeDTO);
        bookService.updatetype(bookTypeDTO);
        return Result.success();
    }

    @DeleteMapping("/type/{id}")
    private Result daletetype(@PathVariable("id") Integer id){
        log.info("删除分类 id = " + id);
        bookService.deletetype(id);
        return Result.success();
    }

    //获取图书分类名
    @GetMapping("/type")
    private Result typename(){
        log.info("获取分类名");
        List<BookType> bookType = bookService.getTypeName();
        return Result.success(bookType);
    }

    /*根据id查图书信息
    * 前端传bookId
    * 获取全部信息除后四个
    * 查询图书信息
    * */
    @GetMapping("/{id}")
    private Result getBookById(@PathVariable("id") Integer id){
        log.info("根据id查询图书信息"+id);
        Book book = bookService.getBookById(id);
        return Result.success(book);
    }

    /*删除图书*/
    @PostMapping("/deletebook")
    private Result delebook(@RequestBody Book book){
        log.info("删除图书{}",book);
        bookService.delebook(book);
        return Result.success();
    }

    /*更新图书信息*/
    @PutMapping("/updatebook")
    private Result updatebook(@RequestBody Book book){
        log.info("修改图书信息：{}",book);
        bookService.updatebook(book);

        return Result.success();
    }
}
